package com.jf.cloud.service.sysmanager.remote.api;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jf.cloud.service.sysmanager.common.util.HttpServletUtil;
import com.jf.cloud.service.sysmanager.dao.ISysAppVersionService;
import com.jf.cloud.service.sysmanager.dao.ISysRoleService;
import com.jf.cloud.service.sysmanager.dao.ISysRoleTypeService;
import com.jf.cloud.service.sysmanager.dao.ISysStaffService;
import com.jf.cloud.service.sysmanager.support.entity.Result;
import com.jf.cloud.service.sysmanager.support.web.BaseController;

/**
 * app版本控制
 * 
 * @author 朱建文 创建时间：2018年4月17日 上午9:58:19
 */
@Controller
@RequestMapping("/appVersion")
public class AppVersionController extends BaseController {
	
	private static Log log = LogFactory.getLog(AppVersionController.class);
	
	@Resource
	private ISysAppVersionService appVersionService;
	
	@Resource
	private ISysStaffService staffService;
	
	@Resource
	private ISysRoleService roleService;

	@Resource
	private ISysRoleTypeService roleTypeService;

	/**
	 * 强制更新
	 */
	@ResponseBody
	@RequestMapping("/checkUpgrade")
	public Result<Map<String, Object>> checkUpgrade(HttpServletRequest request, HttpServletResponse response) {
		HttpServletUtil.setCharacterEncoding(request);
		Result<Map<String, Object>> result = new Result<Map<String, Object>>();
		result.setCode(-1);
		result.setMessage("获取版本更新信息出错");
		try {
			int appId = getParamValueInteger(request, "appId", 3); // 3: Android Phone 默 认安卓

			// 版本号
			String verCode = getParamValueString(request, "verCode");
			if(StringUtils.isEmpty(verCode)){
				result.setMessage("verCode参数缺少");
				return result;
			}

			String channel=getParamValueString(request, "channel", "");
			if(StringUtils.isEmpty(channel)){
				result.setMessage("channel参数缺少");
				return result;
			}

			// 请求查询数据，处理结果处理
			Map<String, Object> map=appVersionService.getCanUseUpgradeInfo(appId, Integer.valueOf(verCode), channel);
			if (map == null || map.isEmpty()) {
				result.setMessage("没有新版本");
				return result;
			}
			
			//版本是否可用---版本是否开启更新功能
			String verState = map.get("verState").toString();
			if("0".equals(verState)){
				log.info("检查更新【checkUpgrade】，没有开启更新功能");
				result.setMessage("没有开启更新功能");
				return result;
			}
			
			//不更新的旧版本号组合
			String oldNotUpdate = (String)map.get("oldNotUpdate");
			if (oldNotUpdate != null && oldNotUpdate.contains(verCode)) {
				log.info("检查更新【checkUpgrade】，本次版本号不更新-----提示没有开启更新功能");
				result.setMessage("没有开启更新功能");
				return result;
			}
			
			//强制更新的版本号组合
			String oldVerForce = (String)map.get("oldVerForce");
			String isUpdate="false";
			if (oldVerForce != null && oldVerForce.contains(verCode)) {
				isUpdate = "true";
			}
			
			// 强制更新
			map.put("isUpdate",isUpdate);
			map.remove("oldNotUpdate");
			map.remove("oldVerForce");
			map.put("desc", map.get("verDesc"));
			map.remove("verDesc");
			
			result.setCode(0);
			result.setData(map);
			result.setMessage("获取版本更新信息成功");
		}catch (Exception e){
			log.error("获取版本更新信息出错!", e);
		}
		return result;
	}

	/**
	 * 检查更新接口  是否切换
	 * 
	 * @author 朱建文 创建时间：2018年4月17日 上午9:57:54
	 */
	@ResponseBody
	@RequestMapping("/getPaymentChannelInfo")
	public Result<String> getPaymentChannelInfo(HttpServletRequest request, HttpServletResponse response) {
		HttpServletUtil.setCharacterEncoding(request);
		Result<String> result = new Result<String>();
		result.setCode(-1);
		result.setMessage("获取版本更新信息出错");
		try {
			int appId = getParamValueInteger(request, "appId", 3); // 3: Android Phone 默 认安卓

			String appType = getParamValueString(request, "appType");// 设备来源
			if(StringUtils.isEmpty(appType)){
				result.setMessage("appType参数缺少");
				return result;
			}
			// 版本号
			String verCode = getParamValueString(request, "verCode");
			if(StringUtils.isEmpty(verCode)){
				result.setMessage("verCode参数缺少");
				return result;
			}

			String channel=getParamValueString(request, "channel", "");
			if(StringUtils.isEmpty(channel)){
				result.setMessage("channel参数缺少");
				return result;
			}
			String verName = getParamValueString(request, "verName", "");

			String controlFlag = "ahskljdhfiuawehfkjashdfuiawheflkjhasiufhaewlifsdgahsdlkjgvhalsiudhglakjsdhgviuaweglkjvbsdzkvjgaewgbvkjadsbgvkuagdslkjvbszdjkhgfliasdbvkjesgiuafgbkljsdbvl";

			if(StringUtils.isNotEmpty(appType)&&StringUtils.isNotEmpty(channel)&&verCode != null){
				controlFlag = appVersionService.getControlFlag(appId, verCode, channel, appType, verName);
			}
			result.setCode(0);
			result.setData(controlFlag);
			result.setMessage("获取版本更新信息成功");
			return result;
		}catch (Exception e){
			log.error("获取版本更新信息出错!", e);
		}
		return result;
	}

}
